package no.uib.hplims;

import no.uib.hplims.models.ActionLock;
import no.uib.hplims.models.Role;
import no.uib.hplims.models.Rule;
import no.uib.hplims.models.RuleFactory;
import no.uib.hplims.models.RuleFactory.FLAG_TYPE;
import no.uib.hplims.models.RuleFactory.POSITION;
import no.uib.hplims.models.RuleFactory.RULE_META;
import no.uib.hplims.models.RuleFactory.RULE_TYPE;
import no.uib.hplims.models.Species;
import no.uib.hplims.models.User;
import no.uib.hplims.tools.SecurityUtil;

import org.vaadin.appfoundation.authentication.exceptions.PasswordsDoNotMatchException;
import org.vaadin.appfoundation.authentication.exceptions.UsernameExistsException;
import org.vaadin.appfoundation.persistence.facade.FacadeFactory;

/**
 * This class's only purpose is to import initial test data to the application.
 * 
 * @author John
 * 
 */
public class InitialData {

	@SuppressWarnings("unused")
	public static void init() {
		if ((FacadeFactory.getFacade().count(Role.class) == 0)
				&& (FacadeFactory.getFacade().count(User.class) == 0)) {
			// Create the admin role
			Role adminRole = new Role();
			adminRole.setName("Administrator");
			for (ActionLock lock : ActionLock.values()) {
				adminRole.addLock(lock);
			}

			FacadeFactory.getFacade().store(adminRole);

			// Create regular user role
			Role regUserRole = new Role();
			regUserRole.setName("User");

			FacadeFactory.getFacade().store(regUserRole);

			try {
				// Register admin users
				User adminUser = SecurityUtil.createUser("john", "secret",
						"secret", "John Ringø", "jonnar@gmail.com", adminRole);
				adminUser = SecurityUtil.createUser("harald", "secret",
						"secret", "Harald Barsnes", "harald.barsnes@gmail.com",
						adminRole);
				// Register other users
				User user = SecurityUtil.createUser("frode", "secret",
						"secret", "Frode", "frode@example.com", regUserRole);
				user = SecurityUtil.createUser("eystein", "secret",
						"secret", "Eystein", "eystein@example.com", regUserRole);
				user = SecurityUtil.createUser("anka", "secret",
						"secret", "Anka", "anka@example.com", regUserRole);
				user = SecurityUtil.createUser("jill", "secret",
						"secret", "Jill", "jill@example.com", regUserRole);
				user = SecurityUtil.createUser("yngvild", "secret",
						"secret", "Yngvild", "yngvild@example.com", regUserRole);
				user = SecurityUtil.createUser("elise", "secret",
						"secret", "Elise", "elise@example.com", regUserRole);
				user = SecurityUtil.createUser("olav", "secret",
						"secret", "Olav", "olav@example.com", regUserRole);
			} catch (UsernameExistsException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (PasswordsDoNotMatchException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Species sp = new Species("Homo sapiens");
			FacadeFactory.getFacade().store(sp);
		}

		createRules();
	}

	private static void createRules() {
		if (FacadeFactory.getFacade().count(Rule.class) == 0) {
			// Create rules
			RuleFactory r = new RuleFactory();
			r.createNewRule(FLAG_TYPE.RED, RULE_TYPE.LENGTH, null,
					RULE_META.DEFAULT, "Sequence length between 6 and 20 AAs",
					null, -1, 6, 20);
			r.createNewRule(FLAG_TYPE.RED, RULE_TYPE.ALLOW, POSITION.SEQUENCE,
					RULE_META.DEFAULT, "Allow K. Max 1 in sequence.", "K", 1,
					-1, -1);
			r.createNewRule(FLAG_TYPE.RED, RULE_TYPE.ALLOW, POSITION.SEQUENCE,
					RULE_META.DEFAULT, "Allow R. Max 1 in sequence.", "R", 1,
					-1, -1);
			r.createNewRule(FLAG_TYPE.RED, RULE_TYPE.DISALLOW,
					POSITION.SEQUENCE, RULE_META.DEFAULT,
					"Disallow M in sequence.", "M", 0, -1, -1);
			r.createNewRule(FLAG_TYPE.RED, RULE_TYPE.DISALLOW,
					POSITION.SEQUENCE, RULE_META.DEFAULT,
					"Disallow C in sequence.", "C", 0, -1, -1);
			r.createNewRule(FLAG_TYPE.RED, RULE_TYPE.DISALLOW,
					POSITION.N_TERMINAL, RULE_META.DEFAULT,
					"Disallow Q in N-terminal.", "Q", 0, -1, -1);
		}
	}
}
